/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.vcforms.dtseditor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import org.opu.vcforms.template.util.ArchiveUtil;
import org.opu.vcforms.transform_t2d.util.DocxUtil;

/**
 *
 * @author yura
 */
public class DocxXmlContentEditor extends JPanel implements ContentEditor {

    /** Contain tabbed pane with xmls and view (2 tabs) */
    private JTabbedPane view = new JTabbedPane(JTabbedPane.BOTTOM);
    /** Contain xml editors */
    private JTabbedPane xmls = new JTabbedPane();

    private ArchiveUtil archiveUtil = new ArchiveUtil();
    private DocxUtil docxUtil = new DocxUtil();
    private Map<String, byte[]> archive;

    public DocxXmlContentEditor(HelpPanel helpPanel, byte[] content) 
            throws IOException {
        super(new BorderLayout());

        ByteArrayInputStream in = new ByteArrayInputStream(content);
        archive = archiveUtil.readArchive(in);
        
        Set<String> documents = docxUtil.documents(archive);
        NullHelpPanel nullHelpPanel = new NullHelpPanel();
        for (String name : documents) {
            byte[] data = archive.get(name);
            xmls.addTab(name, null, new XMLContentEditor(nullHelpPanel, data), name);
        }

        view.addTab("XML", xmls);

        try {
            String str = new String(new XmlUtil().toHTML(archive.get("word/document.xml")));
            JEditorPane editPan = new JEditorPane();
            editPan.setBackground(Color.WHITE);
            editPan.setContentType("text/html");
            editPan.setText(str);
            view.addTab("Document", new JScrollPane(editPan));
        } catch (Exception ex) {
            throw new IOException(ex);
        }

        add(view);
    }

    @Override
    public byte[] getContent() {
        for(int i = 0; i < xmls.getTabCount(); i++){
            XMLContentEditor tab = (XMLContentEditor) xmls.getComponentAt(i);
            String name = xmls.getTitleAt(i);

            archive.put(name, tab.getContent());
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            archiveUtil.writeArchive(archive, out);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

        return out.toByteArray();
    }

    @Override
    public JPanel asPanel() {
        return this;
    }

    class NullHelpPanel extends HelpPanel {

        public NullHelpPanel() {
            super(null);
        }

        @Override
        public void setActive(boolean active) {
        }
    }
}
